home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / MATH / MFLOAT10.ZIP / POLAR.CPP < prev    next >
C/C++ Source or Header  |  1993-04-28  |  2KB  |  57 lines

  1. /* ** This program demonstrates the conversion of cartesian coordinates ** */
  2. /* ** to polar coordinates with mfloat numbers                          ** */
  3.  
  4. /* if you use the mfloat-library use 'polar.prj', otherwise dont use the */
  5. /* project file */
  6.  
  7. #define use_mfloat
  8.  
  9. extern unsigned _stklen = 40000U;
  10.  
  11. #include <conio.h>
  12. #include <stdio.h>
  13. #include <iomanip.h>
  14.  
  15. #ifdef use_mfloat
  16. #include "mfloat.cxx"                   // use the mfloat-library
  17. #else
  18. #include <math.h>                       // use the standard library
  19. #define mfloat double                   // use 'normal' float
  20. #endif
  21.  
  22. mfloat x, y, r, phi, xr, yr;
  23.  
  24. int main() {
  25.   clrscr();
  26.   cout << "          Conversion cartesian coorinates - polar coordinates\n";
  27.   cout << "          ===================================================\n";
  28.   cout << "\n\n";
  29.   cout << "Cartesian coordinates:\n";
  30.   do {
  31.     cout << "x   = ";
  32.     cin.clear();
  33.     cin  >> x;
  34.   } while (cin.rdstate() != ios::goodbit);
  35.   do {
  36.     cout << "y   = ";
  37.     cin.clear();
  38.     cin >> y;
  39.   } while (cin.rdstate() != ios::goodbit);
  40.   cout << "\nx   = " << setprecision(20) << x
  41.        << "\ny   = " << setprecision(20) << y;
  42.  
  43.   r = hypot(x,y);
  44.   phi = atan2(y,x);
  45.   cout << "\n\nConversion to polar coordinates:"
  46.        << "\nr   = " << setprecision(20) << r
  47.        << "\nphi = " << setprecision(20) << phi;
  48.  
  49.   xr = r * cos(phi);
  50.   yr = r * sin(phi);
  51.   cout << "\n\nConversion back to cartesian coordinates:"
  52.        << "\nx   = " << setprecision(20) << xr
  53.        << "\ny   = " << setprecision(20) << yr
  54.        << "\n";
  55.  
  56.   return(0);
  57. }